import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)


const originalPush = VueRouter.prototype.push
// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  { // 主页
    path: '/',
    name: 'home',
    component: () => import('../components/index/index.vue')
  },
  { // 登录页面
    path: '/login',
    name: 'login',
    component: () => import('../components/login/login.vue')
  },
  { // 注册页面
    path: '/register',
    name: 'register',
    component: () => import('../components/login/index.vue')
  },
  { // 忘记密码页面
    path: '/forget',
    name: 'forget',
    component: () => import('../components/login/forget.vue')
  },
  {
    path: "/main",
    name: "main",
    redirect: "/main/index",
    component: () => import(/* webpackChunkName: "home" */ "../views/home/main.vue"),
    meta: { isLogin: true },// 添加该字段，表示进入这个路由是需要登录的
    children: [
      //----------空白页面
      {
        path: "blank",
        name: "blank",
        meta: { isLogin: true },// 添加该字段，表示进入这个路由是需要登录的
        component: () => import(/* webpackChunkName: "home" */ "../components/index/blank.vue"),
      },
      // home页面
      {
        path: "index",
        name: "index",
        meta: { isLogin: true },// 添加该字段，表示进入这个路由是需要登录的
        component: () => import(/* webpackChunkName: "home" */ "../views/home/index.vue"),
      },
      // Activity
      {
        path: "activity",
        name: "activity",
        meta: { isLogin: true },// 添加该字段，表示进入这个路由是需要登录的
        component: () => import(/* webpackChunkName: "home" */ "../views/activity/index.vue"),
      },

      // Activity
      {
        path: "statement",
        name: "statement",
        meta: { isLogin: true },// 添加该字段，表示进入这个路由是需要登录的
        component: () => import(/* webpackChunkName: "home" */ "../views/activity/bankStatement.vue"),
      },
      // Activity
      {
        path: "transactions",
        name: "transactions",
        meta: { isLogin: true },// 添加该字段，表示进入这个路由是需要登录的
        component: () => import(/* webpackChunkName: "home" */ "../views/activity/transactions.vue"),
      },

      // getPaid
      {
        path: "pay",
        name: "pay",
        meta: { isLogin: true },// 添加该字段，表示进入这个路由是需要登录的
        component: () => import(/* webpackChunkName: "home" */ "../views/getPaid/index.vue"),
      },
      //--getPaid
      {
        path: "send",
        name: "send",
        meta: { isLogin: true },// 添加该字段，表示进入这个路由是需要登录的
        component: () => import(/* webpackChunkName: "home" */ "../views/getPaid/send.vue"),
      },
      //--getPaid
      {
        path: "payouts",
        name: "payouts",
        meta: { isLogin: true },// 添加该字段，表示进入这个路由是需要登录的
        component: () => import(/* webpackChunkName: "home" */ "../views/getPaid/payouts.vue"),
      },
      {
        path: "payout-real",
        name: "payout-real",
        meta: { isLogin: true },// 添加该字段，表示进入这个路由是需要登录的
        component: () => import(/* webpackChunkName: "home" */ "../views/getPaid/payout-real.vue"),
      },
      // Activity
      {
        path: "marketing",
        name: "marketing",
        meta: { isLogin: true },// 添加该字段，表示进入这个路由是需要登录的
        component: () => import(/* webpackChunkName: "home" */ "../views/marketing/index.vue"),
      },
    ]
  }
]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.matched.some(res => res.meta.isLogin)) {//判断是否需要登录
    if (localStorage['token']) {
      next();
    } else {
      next({
        path: "/login"
      });
    }
  } else {
    next()
  }
})

export default router
